class Node:
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

    def insert(self, new_data):
    # 将新值与父节点进行比较
        if self.data:  # 非空
            if new_data < self.data:            #新值较小，放左边
                if self.left is None:       #若空，则新建插入节点
                    self.left = Node(new_data)
                else:                       #否则，递归往下查找
                    self.left.insert(new_data)
            elif new_data > self.data:          #新值较大，放右边
                if self.right is None:      #若空，则新建插入节点
                    self.right = Node(new_data)
                else:                       #否则，递归往下查找
                    self.right.insert(new_data)
        else:
            self.data = new_data

    # 打印这棵树，中序遍历
    def PrintTree(self):
        if self.left:
            self.left.PrintTree()
        print( self.data),
        if self.right:
            self.right.PrintTree()

#method2
class BinaryTree(object):
    def __init__(self,item):
        self.key=item
        self.leftChild=None
        self.rightChild=None
    def insertLeft(self,item):
        if self.leftChild==None:
            self.leftChild=BinaryTree(item)
        else:
            t=BinaryTree(item)
            t.leftChild=self.leftChild
            self.leftChild=t
    def insertRight(self,item):
        if self.rightChild==None:
            self.rightChild=BinaryTree(item)
        else:
            t=BinaryTree(item)
            t.rightChild=self.rightChild
            self.rightChild=t

    def PrintTree(self):
        if self.leftChild:
            self.leftChild.PrintTree()
        print( self.key),
        if self.rightChild:
            self.rightChild.PrintTree()


# 使用insert方法添加节点
if __name__=="__main__":
    root = Node(12)
    root.insert(6)
    root.insert(14)
    root.insert(3)

    root.PrintTree()

    #method2
    # 使用insert方法添加节点
    # root = BinaryTree(12)
    # root.insertLeft(6)
    # root.insertRight(14)
    # root.insertRight(3)
    #
    # root.PrintTree()
